from fastapi import APIRouter, Depends
from loguru import logger
from sqlalchemy.orm import Session
from starlette.requests import Request

from apis_module.service.group_service import GroupService
from config.get_db import get_db
from utils.response_util import response_500, response_200

groupMapping = APIRouter()
groupService = GroupService()


@groupMapping.get("/groups")
def get_groups(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = groupService.get_groups_service(query_db)
        return response_200(data=result)
    except Exception as e:
        logger.error(e)
        return response_500(data="", message="服务异常，请联系管理员")


@groupMapping.get("/groups/selector")
def get_groups_selector(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = groupService.get_group_selector_service(query_db)
        return response_200(data=result)
    except Exception as e:
        logger.error(e)
        return response_500(data="", message="服务异常，请联系管理员")
